﻿using System;
using System.Data;
using System.Web.UI;
using Besture.Bll;
using Besture.Common;
using Besture.Common.Helper;
using Besture.Model;
using Besture.UI.ClassHelper;
using DevExpress.Web.ASPxGridView;

namespace Besture.UI.Admin.Control
{
    public partial class BatchModifyStatusControl : System.Web.UI.UserControl
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //绑定酒店份数据
                DataTable tableProvince = QueryDac.GetTable("select * from TBHotels where IsDeleted=0 order by SortID");
                ControlHelper.BindDropDownList(ddlUpdateHotels, tableProvince, "HotelsName", "ID");

                //AspxGridViewHelper.InitAspxGridView(grid);                   

                //绑定房态
                var dt = new DictionaryTypeDac().GetDataTableByType(Constants.DICTIONARY_TYPE_ROOM_STATUS);
                ControlHelper.BindDropDownList(ddlRoomStatus, dt, "Name", "Code", "不做修改");
            }
        }

        #region 删除数据 getBtnBlue();

        /// <summary>
        /// AspxGridView回发事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void GridCustomCallback(object sender, ASPxGridViewCustomCallbackEventArgs e)
        {
            //if (e.Parameters == "Refresh")
            //grid.DataBind();

        }

        /// <summary>
        /// 删除选中数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void LbDelClick(object sender, EventArgs e)
        {/*
            var ids = AspxGridViewHelper.GetCheckID(grid);
            if (string.IsNullOrEmpty(ids))
            {
                return;
            }
            DataTable dt = (DataTable)ViewState["_dt"];
            dt = dt.Select("ID not in(" + ids + ")").Length > 0 ? dt.Select("ID not in(" + ids + ")").CopyToDataTable() : new DataTable();
*/

            /*grid.DataSource = dt;
            grid.DataBind();*/
            //ViewState["_dt"] = grid.DataSource;
            //绑定数据后重新注册一下JS事件
            //GetRegister();

        }


        #endregion



        #region 查询 保存

        //保存 
        protected void BtnSubmitClick(object sender, EventArgs eventArgs)
        {
            var dac = new RoomPriceDac();
            var baseDac = new BaseDac<RoomPriceModel>();
            int lsRecord = 0;

            if (!string.IsNullOrEmpty(hidRoomID.Value.Trim()))
            {
                var roomIds = hidRoomID.Value.TrimEnd(',').Split(',');
                for (int i = 0; i < roomIds.Length; i++)
                {
                    //获取开始时间到结束时间的天数
                    TimeSpan iDataCount = Convert.ToDateTime(Convert.ToDateTime(hidEndRoomTime.Value).ToString("yyyy/MM/dd")) -
                        Convert.ToDateTime(Convert.ToDateTime(hidStartRoomTime.Value).ToString("yyyy/MM/dd"));
                    //获取开始时间
                    DateTime iData = Convert.ToDateTime(hidStartRoomTime.Value);
                    for (int j = 0; j < iDataCount.Days+1; j++) // add 条数
                    {
                        //开始时间是否小于结束时间
                        if (iData <= Convert.ToDateTime(hidEndRoomTime.Value))
                        {
                            RoomPriceModel roomPrice = new RoomPriceDac().GetModelByRoomIdAndRoomTime(Convert.ToInt32(roomIds[i]), iData);
                            //修改房态
                            if (roomPrice!=null)
                            {
                                //房态
                                if (!string.IsNullOrEmpty(ddlRoomStatus.Text) && ddlRoomStatus.Text != "0")
                                {
                                    //有房
                                    if (ddlRoomStatus.Text == Constants.HOUSING_STATE_HAVE.ToString())
                                    {
                                        roomPrice.HousingState = Constants.HOUSING_STATE_HAVE;
                                    }
                                    //紧张
                                    if (ddlRoomStatus.Text == Constants.HOUSING_STATE_TENSION.ToString())
                                    {
                                        roomPrice.HousingState = Constants.HOUSING_STATE_TENSION;
                                    }
                                    //紧张
                                    if (ddlRoomStatus.Text == Constants.HOUSING_STATE_FULL.ToString())
                                    {
                                        roomPrice.HousingState = Constants.HOUSING_STATE_FULL;
                                    }
                                }
                                //修改房间数量
                                if (!rabNumber.Checked)
                                {
                                    //增加房间
                                    if (rabAddRoom.Checked)
                                    {
                                        roomPrice.RoomQuantity = roomPrice.RoomQuantity +
                                                             Convert.ToInt32(string.IsNullOrEmpty(txtAddNumber.Text.Trim())
                                                                 ? "0"
                                                                 : txtAddNumber.Text.Trim());
                                    }
                                    //减少房间
                                    if (rabCutBack.Checked)
                                    {
                                        if (Convert.ToInt32(string.IsNullOrEmpty(txtCutBackNumber.Text.Trim()) ? "0" : txtCutBackNumber.Text.Trim()) > roomPrice.RoomQuantity)
                                        {
                                            FunHelper.Alert("减少的房间数不能大于当前房间数！");
                                            return;
                                        }
                                        roomPrice.RoomQuantity = roomPrice.RoomQuantity -
                                                             Convert.ToInt32(string.IsNullOrEmpty(txtCutBackNumber.Text.Trim())
                                                                 ? "0"
                                                                 : txtCutBackNumber.Text.Trim());
                                    }
                                }
                                roomPrice.LimitedSale = Convert.ToInt32(txtLimitedSale.Text);//限量售卖
                                dac.UpdateRoomStatusAndRoomTime(roomPrice);
                            }
                            //没有房态数据就进行新增
                            else
                            {
                                RoomPriceModel model = new RoomPriceModel
                                {
                                    RoomID = Convert.ToInt32(roomIds[i]),
                                    RoomTime = iData, //开始日期 
                                    SortID = Convert.ToInt32(SortService.GetSortID("TBRoomPrice", false)),
                                    IsDeleted = 0
                                };
                                //房态
                                if (!string.IsNullOrEmpty(ddlRoomStatus.Text) && ddlRoomStatus.Text != "0")
                                {
                                    //有房
                                    if (ddlRoomStatus.Text == Constants.HOUSING_STATE_HAVE.ToString())
                                    {
                                        model.HousingState = Constants.HOUSING_STATE_HAVE;
                                    }
                                    //紧张
                                    if (ddlRoomStatus.Text == Constants.HOUSING_STATE_TENSION.ToString())
                                    {
                                        model.HousingState = Constants.HOUSING_STATE_TENSION;
                                    }
                                    //紧张
                                    if (ddlRoomStatus.Text == Constants.HOUSING_STATE_FULL.ToString())
                                    {
                                        model.HousingState = Constants.HOUSING_STATE_FULL;
                                    }
                                }
                                //修改房间数量
                                if (!rabNumber.Checked)
                                {
                                    //增加房间
                                    if (rabAddRoom.Checked)
                                    {
                                        model.RoomQuantity = model.RoomQuantity +
                                                             Convert.ToInt32(string.IsNullOrEmpty(txtAddNumber.Text.Trim())
                                                                 ? "0"
                                                                 : txtAddNumber.Text.Trim());
                                    }
                                    //减少房间
                                    if (rabCutBack.Checked)
                                    {
                                        if (Convert.ToInt32(string.IsNullOrEmpty(txtCutBackNumber.Text.Trim()) ? "0" : txtCutBackNumber.Text.Trim()) > model.RoomQuantity)
                                        {
                                            FunHelper.Alert("减少的房间数不能大于当前房间数！");
                                            return;
                                        }
                                        model.RoomQuantity = model.RoomQuantity -
                                                             Convert.ToInt32(string.IsNullOrEmpty(txtCutBackNumber.Text.Trim())
                                                                 ? "0"
                                                                 : txtCutBackNumber.Text.Trim());
                                    }
                                }

                                model.LimitedSale = Convert.ToInt32(txtLimitedSale.Text);//限量售卖
                                baseDac.LoadAddBaseModel(model);
                                dac.Add(model);
                            }
                            //lsRecord += new RoomPriceDac().GetModelByRoomIdAndRoomTime(Convert.ToInt32(roomIds[i]), iData) != null ? dac.UpdateRoomStatusAndRoomTime(model) : dac.Add(model);
                            iData = iData.AddDays(1);
                        }
                    }
                }
            }
            if (lsRecord > 0)
            {
                FunHelper.Alert("保存成功", "/admin/RoomStatus/RoomStatusSet.aspx");
                //ScriptManager.RegisterClientScriptBlock(this.UpdatePanel3, GetType(), "getUpdateHotels", "myValidate();", true);
            }
        }



        // 查询
        protected void BtnSeachClick(object sender, EventArgs e)
        {
            /**
               * 1.根据选中房型数 grid显示多少行
               * 2.日期显示格式 yyyy-MM-dd - yyyy-MM-dd
               * 
               * ***/
            DataTable dt = new DataTable();
            dt.Columns.Add("ID");
            dt.Columns.Add("RoomID");
            dt.Columns.Add("RoomTime");
            dt.Columns.Add("LowPrice");
            dt.Columns.Add("Price");
            dt.Columns.Add("OneDealerPrice");
            dt.Columns.Add("TwoDealerPrice");
            dt.Columns.Add("ThreeDealerPrice");
            var vRoomTime = hidStartRoomTime.Value + " 至 " + hidEndRoomTime.Value;
            var vLowPrice = 1;
            string[] sRoomId = hidRoomID.Value.TrimEnd(',').Split(',');
            for (int i = 0; i < sRoomId.Length; i++)
            {
                //酒店id 和房型id 得到 房型低价 
                var vRoomModel = new RoomDac().GetModel(Convert.ToInt32(sRoomId[i]));
                if (vRoomModel != null)
                    vLowPrice = vRoomModel.LowPrice;

                DataRow newdr = dt.NewRow();
                newdr["ID"] = i + 1;
                newdr["RoomID"] = sRoomId[i];
                newdr["RoomTime"] = vRoomTime;
                newdr["LowPrice"] = vLowPrice;
                newdr["Price"] = vLowPrice; //初始化价格 = 低价
                newdr["OneDealerPrice"] = vLowPrice; //初始化价格 = 低价
                newdr["TwoDealerPrice"] = vLowPrice; //初始化价格 = 低价
                newdr["ThreeDealerPrice"] = vLowPrice; //初始化价格 = 低价
                dt.Rows.Add(newdr);
            }
            if (dt.Rows.Count > 0)
            {
                /*grid.DataSource = dt;
                grid.DataBind();
                ViewState["_dt"] = grid.DataSource;   */
            }
            //绑定数据后重新注册一下JS事件
            GetRegister();
        }



        /// <summary>
        /// 绑定数据后重新注册一下JS事件
        /// </summary>
        private void GetRegister()
        {
            //绑定数据后重新注册一下JS事件
            ScriptManager.RegisterClientScriptBlock(this.UpdatePanel3, GetType(), "getUpdateHotels", "getUpdateHotels();setTab(this, 1);getBtnBlue();", true);
        }


        #endregion


        #region 初始化  早餐情况 房型 名称

        /// <summary>
        /// 早餐情况
        /// </summary>
        /// <returns></returns>
        public string GetStringBreakfast()
        {
            //早餐情况 绑定
            var vBreakfast = QueryDac.GetTable("SELECT * FROM TBDictionaryType WHERE Type = 5");
            string sHtml = "<select id='slBreakfast' runat='server' >";
            foreach (DataRow dr in vBreakfast.Rows)
            {
                sHtml += string.Format("<option value={0}>{1}</option>", dr["Code"], dr["name"]);
            }
            return (sHtml + "</select>");
        }


        /// <summary>
        /// 房型 名称
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public string GetRoomNameById(string id)
        {
            string sRoomName = string.Empty;
            var model = new RoomDac().GetModel(Convert.ToInt32(id));
            if (model != null)
            {
                sRoomName = model.RoomName;
            }
            return sRoomName;
        }

        #endregion

    }
}
